package me.pcqpcq.android.aclean;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.pcqpcq.android.Constant;
import me.pcqpcq.android.aclean.R;
import me.pcqpcq.android.adapter.SimpleCleanAdapter;
import me.pcqpcq.android.msg.MessageCentre;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ListView;

/**
 * 垃圾清理的黑名单管理
 * @author pcqpcq
 *
 */
public class BlackListActivity extends Activity implements OnClickListener {

	private SimpleCleanAdapter blackListAdapter;
	private List<Map<String, Object>> blackList = new ArrayList<Map<String, Object>>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.layout_dirs_list);

		blackListAdapter =
				new SimpleCleanAdapter(BlackListActivity.this, blackList, R.layout.listview_item, new String[] { "fileicon",
						"filename" }, new int[] { R.id.listview_icon, R.id.listview_textview });

		ListView blackListView = (ListView) findViewById(R.id.dirslist_list);
		blackListView.setAdapter(blackListAdapter);

		// 测试测试测试测试测试测试测试测试
		String[] filePath = { "/", "/sdcard", "/sdcard/Download" };
		for (String path : filePath)
			updateBlackList(path);
	}

	/**
	 * <p>将指定的路径更新到View中的列表
	 * @param filePath 指定的路径 
	 */
	private void updateBlackList(String filePath) {
		File file = new File(filePath);
		Map<String, Object> map = new HashMap<String, Object>();
		if (file.isDirectory())
			map.put("fileicon", R.drawable.ic_folder);
		else
			map.put("fileicon", R.drawable.ic_file);
		map.put("filename", file.getAbsoluteFile());
		map.put("file", file);
		blackList.add(map);
		blackListAdapter.notifyDataSetChanged();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Constant.RESULT_CODE_PICK_DIR) {
			SharedPreferences cleanList =
					getSharedPreferences(Constant.PREFERENCE_CLEAN_LIST, Context.MODE_PRIVATE);
			SharedPreferences.Editor editor = cleanList.edit();
			editor.putString("black_list", data.getStringExtra("curpath"));
			editor.putInt("black_list_total", 0);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.dirslist_btn_add:
			if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
				Intent pickDirIntent = new Intent();
				pickDirIntent.setClass(BlackListActivity.this, PickDirActivity.class);
				pickDirIntent.putExtra("filepath", Environment.getExternalStorageDirectory().getAbsolutePath());
				startActivityForResult(pickDirIntent, Constant.RESULT_CODE_PICK_DIR);
			} else
				MessageCentre.showShortToast(getString(R.string.toast_no_sdcard));
			break;

		case R.id.dirslist_btn_remove:
			MessageCentre.showShortToast(getString(R.string.toast_no_sdcard));
			break;

		case R.id.dirslist_btn_ok:
			finish();
			break;

		case R.id.listview_check:
			MessageCentre.showShortToast(getString(R.string.toast_no_sdcard));
			break;

		default:
			break;
		}
	}

}
